﻿/*
Copyright (c) 2005-2008, CellAO Team

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CellAO Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using AO.Core;

/*
 *
 * 
 * 
 *          PLEASE DO NOT TOUCH THIS CODE RIGHT NOW!
 *          DNIFAN IS WORKING ON IT AND YOU MIGHT MESS IT UP SO FAR HE
 *          DOESN'T KNOW HIS WAY AROUND ANYMORE :P
 *          
 *          Thanks!
 * 
 *          P.S. Suiv was here >:) 
 * 
 */

namespace ZoneEngine.NPC
{
    /// <summary>
    /// Base NPC type, other NPC type is LuaNPC, which will be derived from this one
    /// </summary>
    public class NPCType
    {
        /// <summary>
        /// 
        /// </summary>
        public NPCType()
        {
            DynelID = Misc.DynelHandler.GetDynelID();
        }

        /// <summary>
        /// Broadcast to all players in PlayField of client 'c'
        /// </summary>
        /// <param name="c">The client</param>
        virtual public void BroadcastPF(Client c)
        {
            foreach (Client cli in c.Server.Clients)
            {
                SendToClient(cli);
            }
        }

        /// <summary>
        /// Sends the NPC to the client, HAS to be overridden.
        /// </summary>
        /// <param name="c">The client.</param>
        virtual public void SendToClient(Client c)
        {

        }

        /// <summary>
        /// Set Position of the NPC.
        /// </summary>
        /// <param name="coordinates">Coordinates</param>
        public void SetPosition(AOCoord coordinates)
        {
            coord = coordinates;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="heading"></param>
        public void SetHeading(Quaternion heading)
        {
            this.heading = heading;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lol"></param>
        /// <returns></returns>
        virtual public bool NPCInit(int lol)
        {
            //empty, but every NPC type needs a 'Init' function.
            return false;
        }

        /// <summary>
        /// This sends the packet to follow a client (broadcasts to PF)
        /// </summary>
        /// <param name="c">The client to follow</param>
        virtual public void Follow(Client c)
        {
            PacketWriter _writer = new PacketWriter();

            #region Header

            _writer.PushByte(0xDF); _writer.PushByte(0xDF);
            _writer.PushShort(10);
            _writer.PushShort(1);
            _writer.PushShort(0); //size
            _writer.PushInt(3086);
            _writer.PushInt(c.Character.characterId);
            _writer.PushInt(0x260F3671);
            _writer.PushIdentity(50000, DynelID);
            _writer.PushByte(0);

            #endregion

            _writer.PushBytes(new byte[]
            {
                0x01, 0x19, 0x02,
                0x44, 0x52, 0x32, 0xA3, 0x42, 0x0F, 0x26, 0xE5,   
                0x44, 0x4C, 0xD6, 0xE3, 0x44, 0x53, 0x29, 0x44,
                0x42, 0x0E, 0x4E, 0x7B, 0x44, 0x4D, 0xE7, 0x92,
            });

            byte[] reply = _writer.Finish();
            /*c.CmpStream.Write(reply, 0, reply.Length);
            c.CmpStream.Flush();*/

            Misc.Announce.Playfield(c, reply);
        }

        public int pf;
        public string Name;
        public AOCoord coord = new AOCoord();
        public Quaternion heading = new Quaternion(0, 0, 0, 1);
        public short Health;
        public short Level;

        public int Side;
        public int Breed;
        public int Face;
        public int Fatness;
        public int Sex;
        public int Race;

        public int MonsterData;
        public short MonsterScale = 0x0060;
        public int RunningSpeed;

        public int DynelID;
    }
}
